package dao

import (
	"219yk/inits"
	"219yk/models"
	"gorm.io/gorm"
)

func AddVideo(video models.Video) error {
	return inits.DB.Debug().Create(&video).Error
}

func AddConRen(ren models.ConRen) error {
	return inits.DB.Debug().Create(&ren).Error
}

func GetVideo(videoId int) bool {
	var good models.Video
	err := inits.DB.Debug().Where("id = ?", videoId).Limit(1).Find(&good).Error
	if err != nil {
		return false
	}
	return true
}

func CreateGood(good models.Good) bool {
	err := inits.DB.Debug().Create(&good).Error
	if err != nil {
		return false
	}
	return true
}

func DeletedGood(good models.Good) bool {
	err := inits.DB.Debug().Where(&good).Delete(&models.Good{}).Error
	if err != nil {
		return false
	}
	return true
}

func AddVideoGood(id int) error {
	return inits.DB.Debug().Model(&models.Video{}).Where("id = ?", id).Update("good_num", gorm.Expr("good_num + ?", 1)).Error

}

func DelVideoGood(id int) error {
	return inits.DB.Debug().Model(&models.Video{}).Where("id  = ?", id).Update("good_num", gorm.Expr("good_num - ?", 1)).Error
}

func CreateComment(comment models.Comment) bool {
	err := inits.DB.Debug().Create(&comment).Error
	if err != nil {
		return false
	}
	return true
}

func CreateCommentNum(id int) error {
	return inits.DB.Debug().Model(&models.Video{}).Where("id = ?", id).Update("comment_num", gorm.Expr("comment_num + ?", 1)).Error
}

func ListVideo() ([]models.Video, error) {
	video := []models.Video{}
	err := inits.DB.Debug().Order("good_num desc").Find(&video).Error
	if err != nil {
		return []models.Video{}, err
	}
	return video, err
}

func GoodList() ([]models.Good, error) {
	good := []models.Good{}
	err := inits.DB.Debug().Find(&good).Error
	if err != nil {
		return []models.Good{}, err
	}
	return good, err
}

func CommentList() ([]models.Comment, error) {
	var comment []models.Comment
	err := inits.DB.Debug().Find(&comment).Error
	if err != nil {
		return []models.Comment{}, err
	}
	return comment, err
}
